package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.domain.BaseEntity;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.system.domain.DyPaySetting;
import com.ruoyi.system.domain.bo.DyPaySettingBo;
import com.ruoyi.system.domain.vo.*;
import com.ruoyi.system.mapper.*;
import com.ruoyi.system.service.IDyPaySettingService;
import lombok.RequiredArgsConstructor;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * 主播Service业务层处理
 *
 * @author ruoyi
 * @date 2024-06-20
 */
@RequiredArgsConstructor
@Service
@Transactional(rollbackFor = Exception.class)
public class DyPaySettingServiceImpl implements IDyPaySettingService {

    private final DyPaySettingMapper dyPaySettingMapper;

    @Override
    public Boolean insertByBo(DyPaySettingBo bo) {

        DyPaySetting pay = BeanUtil.toBean(bo, DyPaySetting.class);

        return dyPaySettingMapper.insert(pay) > 0;
    }

    @Override
    public Boolean updateByBo(DyPaySettingBo bo) {

        if (bo.getId() == null){
            throw new ServiceException("查询id参数为空！");
        }

        DyPaySetting pay = dyPaySettingMapper.selectById(bo.getId());

        if (pay == null){
            throw new ServiceException("需要修改的数据已查询不到！");
        }

        pay.setAlipayKey(bo.getAlipayKey());
        pay.setAlipaySec(bo.getAlipaySec());
        pay.setUrl(bo.getUrl());
        pay.setWeChatImg(bo.getWeChatImg());
        return dyPaySettingMapper.updateById(pay) > 0;
    }

    @Override
    public DyPaySettingVo queryById(DyPaySettingBo bo) {


        DyPaySetting pay = dyPaySettingMapper.selectOne(new QueryWrapper<DyPaySetting>().lambda().orderByDesc(BaseEntity::getCreateTime).last(" limit 1"));

        if (pay == null){
            throw new ServiceException("需要修改的数据已查询不到！");
        }

        DyPaySettingVo vo = new DyPaySettingVo();

        if (bo.getType() == null){
            vo = null;
        }
        else{
            switch (bo.getType()){
                case 0:
                    vo = BeanUtil.toBean(pay, DyPaySettingVo.class);
                case 1:
                    vo.setAlipayKey(pay.getAlipayKey());
                case 2:
                    vo.setAlipaySec(pay.getAlipaySec());
                case 3:
                    vo.setUrl(pay.getUrl());
                case 4:
                    vo.setWeChatImg(pay.getWeChatImg());
            }
        }

        return vo;
    }
}
